package com.peng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.peng.dao.mapper.TagMapper;
import com.peng.dao.pojo.Tag;
import com.peng.service.TagService;
import com.peng.vo.Result;
import com.peng.vo.TagVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class TagServiceImpl implements TagService {
    @Autowired
    private TagMapper mapper;

    public List<TagVo> copyList(List<Tag> records) {
        ArrayList<TagVo> tagVos = new ArrayList<>();
        for (Tag record : records) {
            tagVos.add(copy(record));
        }
        return tagVos;
    }

    private TagVo copy(Tag record) {
        TagVo tagVo = new TagVo();
        BeanUtils.copyProperties(record,tagVo);
        return tagVo;
    }
    @Override
    public List<TagVo> findTagsByArticleId(Long id) {
        List<Tag> tags = mapper.findTagsByArticleId(id);
        return copyList(tags);
    }

    @Override
    public Result hots(int limit) {
        List<Long> tagsByTagId = mapper.findTagsByTagId(limit);
        if (tagsByTagId==null){
            return Result.success(Collections.emptyList());
        }
        List<Tag> list= mapper.findTagsByName(tagsByTagId);
        return Result.success(list);
    }

    @Override
    public Result findAll() {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Tag::getId,Tag::getTagName);
        List<Tag> list = mapper.selectList(queryWrapper);
        return Result.success(copyList(list));
    }

    @Override
    public Result findAllDetail() {
        List<Tag> list = mapper.selectList(new LambdaQueryWrapper<>());
        return Result.success(copyList(list));
    }

    @Override
    public Result findAllDetailById(Long id) {
        Tag tag = mapper.selectById(id);
        return Result.success(copy(tag));
    }


}
